<?php

class Person_model extends CI_Model {
	

	public function getCountryAndTag(){
	
		$country_r = $this->db->get('country');
		if($country_r->num_rows){
			$country = $country_r->result_id();
			$country_id = array_keys($country);

			$this->db->where_in('country_id',$country_id);
			$this->db->select('person_id,country_id');
			$country_person = $this->db->get('country_person')->result_id();
			$tag = array();
			if($country_person){

				foreach($country_person as $t){
					$country[$t->country_id]->person_id[] = $t->person_id;
				}
				$country_person_id = array_keys($country_person);
				
				$this->db->where_in('person_id',$country_person_id);
				$this->db->join('tag','tag.id=tag_person.tag_id');
				$this->db->select('tag_person.person_id,tag_person.tag_id,tag.tag_name');
				$tag = $this->db->get('tag_person')->result_id();

				if($tag){
					foreach($tag as $t){
						
					}
				}
			}
			foreach($country as $c){
				if(isset($c->person_id) && is_array($c->person_id)){
					foreach($c->person_id as $pid){
						$c->tag_name[] = $tag[$pid]->tag_name;
					}
					$c->tag_name = array_unique($c->tag_name);
				}else{
					$c->tag_name = array();
					$c->person_id = array();
				}
			}
			//dbg($country);
			return $country;
			
		}
		return array();
	}

	public function getPerson(){
		$person = $this->db->get('person')->result_id();
		return $person;
	}
	public function addPerson(){
		$data['name'] = $this->form_validation->set_value('name');
		$data['website'] = $this->form_validation->set_value('website');
		$data['avatar'] = $this->form_validation->set_value('avatar');
		$data['office'] = $this->form_validation->set_value('office');
		$data['address'] = $this->form_validation->set_value('address');
		$data['email'] = $this->form_validation->set_value('email');
		$data['brif'] = $this->form_validation->set_value('brif');
		$data['introduction'] = $this->form_validation->set_value('introduction');
		$country = $data['country'] = $this->form_validation->set_value('country');
		$data['phone'] = $this->form_validation->set_value('phone');
		$data['fax'] = $this->form_validation->set_value('fax');
		$tags = $data['tag'] = $this->form_validation->set_value('tag');
		$this->db->insert('person',$data);
		$person_id = $this->db->insert_id();
		
		$tag = explode(',',$tags);
		if(!empty($tag)){
			foreach($tag as $t){
				$t = trim($t);
				if(!$t) continue;


				$this->db->where('tag_name',$t);
				$q = $this->db->get('tag',array('tag_name'=>$t));
				if($q->num_rows == 0){
					$this->db->insert('tag',array('tag_name'=>$t));
					$tag_id = $this->db->insert_id();
				}else{
					$tag_id = $q->row()->id;
				}

				$this->db->insert('tag_person',array('tag_id'=>$tag_id,'person_id'=>$person_id));
			}
		}


		if(!empty($country)){
			$t = trim($country);
			if($t){
				$this->db->where('country_name',$t);
				$q = $this->db->get('country',array('country_name'=>$t));
				if($q->num_rows == 0){
					$this->db->insert('country',array('country_name'=>$t));
					$tag_id = $this->db->insert_id();
				}else{
					$tag_id = $q->row()->id;
				}

				$this->db->insert('country_person',array('country_id'=>$tag_id,'person_id'=>$person_id));
			}
		}

		if($person_id) return true;

		return false;
	}


	public function update($id){
		$data['name'] = $this->form_validation->set_value('name');
		$data['website'] = $this->form_validation->set_value('website');
		$data['avatar'] = $this->form_validation->set_value('avatar');
		$data['office'] = $this->form_validation->set_value('office');
		$data['address'] = $this->form_validation->set_value('address');
		$data['email'] = $this->form_validation->set_value('email');
		$data['brif'] = $this->form_validation->set_value('brif');
		$data['introduction'] = $this->form_validation->set_value('introduction');
		$country = $data['country'] = $this->form_validation->set_value('country');
		$data['phone'] = $this->form_validation->set_value('phone');
		$data['fax'] = $this->form_validation->set_value('fax');
		$tags = $data['tag'] = $this->form_validation->set_value('tag');
		$this->db->where('id',$id);
		$up = $this->db->update('person',$data);
		if(!$up) return false;
		$person_id = $id;
		
		$tag = explode(',',$tags);
		if(!empty($tag)){
			foreach($tag as $t){
				$t = trim($t);
				if(!$t) continue;


				$this->db->where('tag_name',$t);
				$q = $this->db->get('tag',array('tag_name'=>$t));
				if($q->num_rows == 0){
					$this->db->insert('tag',array('tag_name'=>$t));
					$tag_id = $this->db->insert_id();
				}else{
					$tag_id = $q->row()->id;
				}
				
				$this->db->where('person_id',$person_id);
				$this->db->delete('tag_person');
				$this->db->insert('tag_person',array('tag_id'=>$tag_id,'person_id'=>$person_id));
			}
		}

		if($up) return true;

		return false;
	}
	public function getPersonByTag($tag){
		$this->db->where('tag_name',$tag);
		$row =$this->db->get('tag')->row();
		if(!$row) return false;

		$this->db->where('tag_id',$row->id);
		$this->db->select('person_id,tag_id');
		$result = $this->db->get('tag_person')->result_id();

		$person_id = array_keys($result);

		$this->db->where_in('id',$person_id);
		$person = $this->db->get('person')->result();

		if($person) return $person;
	}


	public function del($id){
		$this->db->where('id',$id);
		$this->db->delete('person');
	}
	public function getPersonByCountry($tag){
		$this->db->where('country_name',$tag);
		$row =$this->db->get('country')->row();
		if(!$row) return false;

		$this->db->where('country_id',$row->id);
		$this->db->select('person_id,country_id');
		$result = $this->db->get('country_person')->result_id();

		$person_id = array_keys($result);

		$this->db->where_in('id',$person_id);
		$person = $this->db->get('person')->result();

		if($person) return $person;
	}

	public function getLists($start=0,$limit=100){
		$this->db->limit($limit,$start);
		$this->db->order_by('id','desc');
		return $this->db->get('person')->result();
	}

	public function getPersonByID($id){
		return $this->db->get_where('person',array('id'=>$id))->row();
	}
}